import {fabric} from  "@/js/fabric";
let ShDevice = fabric.Group;
// function ShDevice(){
//   fabric.Group.apply(this,arguments);
// }
(function(){
  // // 创建一个没有实例方法的类
  // var Super = function(){};
  // Super.prototype = fabric.Group.prototype;
  // //将实例作为子类的原型
  // ShDevice.prototype = new Super();
  
  ShDevice.prototype.initialize =function(objects, options, isAlreadyGrouped){
    options = options || {};
    this._objects = [];
    // if objects enclosed in a group have been grouped already,
    // we cannot change properties of objects.
    // Thus we need to set options to group without objects,
    isAlreadyGrouped && this.callSuper('initialize', options);
    this._objects = objects || [];
    for (var i = this._objects.length; i--; ) {
      this._objects[i].group = this;
    }

    if (!isAlreadyGrouped) {
      var center = options && options.centerPoint;
      // we want to set origins before calculating the bounding box.
      // so that the topleft can be set with that in mind.
      // if specific top and left are passed, are overwritten later
      // with the callSuper('initialize', options)
      if (options.originX !== undefined) {
        this.originX = options.originX;
      }
      if (options.originY !== undefined) {
        this.originY = options.originY;
      }
      // if coming from svg i do not want to calc bounds.
      // i assume width and height are passed along options
      center || this._calcBounds();
      this._updateObjectsCoords(center);
      delete options.centerPoint;
      this.callSuper('initialize', options);
    }
    else {
      this._updateObjectsACoords();
    }
    this.hotpotslist = getHotpots.call(this);
    let that = this;  
    this.on("removed",function(e){
      clearEdge(that)
    })
  this.setCoords();    
};
function clearWires(target,wires){
  for(let item in target.hotpotslist){
    target.hotpotslist[item]=target.hotpotslist[item].filter(element=>{
      return wires!==element
    })
  }
}
function clearEdge(target){
  for(let item in target.hotpotslist){
    target.hotpotslist[item].forEach(element => {
      element.canvas&&element.canvas.remove(element)
    });
  }
}
let getHotpots =  function(){
  let hotpots={};
  this._objects.map((element,index) => {
    if(element.hotspot==true){
     element.hotid=element.hotid || `l${index}`;
      hotpots[element.hotid]=[]
    }
  });
  return hotpots
}
})();
export default ShDevice